package com.huxt.Recursion.lookou;
/// https://leetcode.com/problems/remove-linked-list-elements/description/

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while(head != null && head.val == val){
            ListNode delNode = head;
            head = head.next;
            delNode.next = null;
        }

        if(head == null)
            return head;

        ListNode prev = head;
        while(prev.next != null){
            if(prev.next.val == val) {
                ListNode delNode = prev.next;
                prev.next = delNode.next;
                delNode.next = null;
            }
            else
                prev = prev.next;
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(4);
        ListNode head1 = new ListNode(4);
        ListNode head2 = new ListNode(4);
        head.next = head1;
        head1.next = head2;
        System.out.println(head.toString());
        Solution solution = new Solution();
        solution.removeElements(head,5);
    }
}